using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;
using Bt.Mvc;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Bt.DoorWebServer.View;
using Bt.SecuritySystem;

namespace Bt.DoorWebServer
{
    public class DoorController
        : Controller
    {
        private OutputPort relayPin = new OutputPort(Pins.GPIO_PIN_D6, true);
        private OutputPort ledPin = new OutputPort(Pins.GPIO_PIN_D13, false);
        private Alerter alerter;
        public DoorController(Alerter alerter)
        {
            this.alerter = alerter;
        }
        
        private Thread t;
        public void OpenDoor() 
        {
            relayPin.Write(false);
            for (int i = 0; i < 60; i++)
            {
                ledPin.Write(!ledPin.Read());
                Thread.Sleep(50);
            }
            ledPin.Write(false);
            relayPin.Write(true);
            t = null;
        }

        public FillableViewResult Arm()
        {
            this.alerter.Arm();

            var result = new FillableViewResult() { Body = ViewsResource.GetString(ViewsResource.StringResources.Index), LogFile = "Log.Read()" };
            result.ViewData = new ViewData() { DateTime = DateTime.UtcNow };
            result.Body += this.DoorState().Body;
            result.Body += this.ArmState().Body;
            return result;
        }
        public FillableViewResult DisArm()
        {
            this.alerter.DisArm();

            var result = new FillableViewResult() { Body = ViewsResource.GetString(ViewsResource.StringResources.Index), LogFile = "Log.Read()" };
            result.ViewData = new ViewData() { DateTime = DateTime.UtcNow };
            result.Body += this.DoorState().Body;
            result.Body += this.ArmState().Body;
            return result;
        }

        public FillableViewResult Open()
        {
            if (t == null) 
            {
                t = new Thread(new ThreadStart(OpenDoor));
                t.Start();
            }

            var result = new FillableViewResult() { Body = ViewsResource.GetString(ViewsResource.StringResources.Open), LogFile = "Log.Read()" };
            result.ViewData = new ViewData() { DateTime = DateTime.UtcNow };
            result.Body += this.DoorState().Body;
            result.Body += this.ArmState().Body;
            return result;
        }
        public FillableViewResult Index()
        {
            var result = new FillableViewResult() { Body = ViewsResource.GetString(ViewsResource.StringResources.Index), LogFile = "Log.Read()" };
            result.Body += this.DoorState().Body;
            result.Body += this.ArmState().Body;
            return result;
        }

        public FillableViewResult DoorState()
        {
            if (this.alerter.IsDoorOpen)
            {
                return new FillableViewResult() { Body = "<span class='label label-danger'>Open</span>" };
            }
            else
            {
                return new FillableViewResult() { Body = "<span class='label label-success'>Close</span>" };
            }
        }

        public FillableViewResult ArmState()
        {
            if (this.alerter.IsArmed)
            {
                return new FillableViewResult() { Body = "<span class='label label-danger'>Armed</span>" };
            }
            else
            {
                return new FillableViewResult() { Body = "<span class='label label-success'>DisArmed</span>" };
            }
        }
    }
}